import { ALIYUN_URL } from '../../utils/config.js'
import { Request, toast } from '../../utils/util.js'
let request = new Request()
Component({
  properties: {
    urls : {
      type : Array,
      value : []
    },

    userId: {
      type: Number,
      value: 0
    }, 

    chatId: {
      type: Number,
      value: 0
    },
    collectId : {
      type : Number,
      value : 0
    },
    collectStatus : {
      type : Boolean,
      value : false
    },

    praiseStatus: {
      type: Boolean,
      value: false
    },

    exclude : {
      type : Array,
      value: []
    }
  },

  lifetimes : {
    attached () {
      let exclude = this.properties.exclude
      this.setData({
        showDown : !exclude.includes('down'),
        showCollect: !exclude.includes('collect'),
        showContact: !exclude.includes('contact'),
        showPraise: !exclude.includes('praise')
      })
    }
  },

  observers : {
    collectId (collectStatus) {
      this.setData({ collect_id: collectId})
    },
    collectStatus(collectStatus) {
      this.setData({ collect_status: collectStatus })
    },
    praiseStatus(praiseStatus) {
      this.setData({ praise_status: praiseStatus })
    }
  },

  data: {
    collect_id : 0,
    collect_status : false,
    praise_status : false,
    showDown : false,
    showCollect : false,
    showContact : false,
    showPraise : false
  },

  methods: {
    down () {
      this.properties.urls.forEach(url => {
        wx.downloadFile({
          url: ALIYUN_URL + '/' + url,
          success : res => {
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success : res => {
                wx.showToast({
                  title: '下载完成'
                })
                console.log(res)
              },
              fail: res => {
                console.log(res)
              }
            })
            console.log(res)
          },
          fail : res => {
            console.log(res)
          }
        })
      })
    },

    collect () {
      let chatId = this.properties.chatId
      let collectStatus = this.data.collect_status
      if (collectStatus){ //取消收藏
        request.get('chat/uncollect', res => {
          if(res.success){
            this.setData({ collect_status : false})
          }else{
            toast(res.msg)
          }
        }, { id: chatId})
      }else{ //收藏
        request.get('chat/collect', res => {
          if (res.success) {
            this.setData({ collect_status: true })
          } else {
            toast(res.msg)
          }
        }, { id: chatId })
      }
    },

    praise() {
      let chatId = this.properties.chatId
      let praiseStatus = this.data.praise_status
      request.get(praiseStatus == false ? 'chat/praise' : 'chat/unpraise', res => {
        if (res.success) {
          this.setData({ praise_status: !praiseStatus})
        } else {
          toast(res.msg)
        }
      }, { id: chatId })
    },

    contact () {
      wx.navigateTo({
        url: '/pages/contact/index?userId=' + this.properties.userId,
      })
    }
  }
})
